package com.freesky.biz.rights.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.freesky.biz.icbc.domain.TAigouFriday;
import com.freesky.biz.icbc.service.IAigouFridayService;
import com.freesky.biz.rights.domain.RightsPayIcbcEntity;
import com.freesky.biz.rights.domain.RightsReceiveEntity;
import com.freesky.biz.rights.mapper.RightsReceiveMapper;
import com.freesky.biz.rights.service.IRightsReceiveService;
import com.freesky.biz.utils.SeqUtil;
import com.freesky.common.core.domain.AjaxResult;
import com.freesky.common.core.domain.entity.SysUser;
import com.freesky.common.enums.ExportStatusEnum;
import com.freesky.common.utils.SecurityUtils;
import com.freesky.common.utils.StringUtils;
import com.freesky.system.domain.TExportRecord;
import com.freesky.system.service.ITExportRecordService;
import com.freesky.system.task.SyncReport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * 领取记录
 */
@Service
public class RightsReceiveServiceImpl extends ServiceImpl<RightsReceiveMapper, RightsReceiveEntity> implements IRightsReceiveService {

    @Autowired
    private RightsReceiveMapper rightsReceiveMapper;

    @Autowired
    private ITExportRecordService exportRecordService;

    @Autowired
    private SyncReport syncReport;


    @Override
    public Page selectReceivePage(Page page, RightsReceiveEntity entity) {
        return rightsReceiveMapper.selectReceivePage(page,entity);
    }

    @Override
    public List<RightsReceiveEntity> selectReceiveList(RightsReceiveEntity entity) {
        return rightsReceiveMapper.selectReceivePage(entity);
    }

    @Override
    public AjaxResult exportFile(RightsReceiveEntity entity) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        TExportRecord exportRecord = new TExportRecord();
        exportRecord.setId(SeqUtil.getSeqId());
        exportRecord.setBizType("RIGHTS_RECEIVE");
        exportRecord.setCreateBy(String.valueOf(sysUser.getUserId()));

        TExportRecord exportRecordSelect = exportRecordService.selectRecordExportByStatus(exportRecord);
        if (exportRecordSelect != null) {
            return AjaxResult.error("已经有下载任务");
        }
        exportRecord.setStatus(ExportStatusEnum.SUBMIT_WAIT.getValue());
        System.out.println(JSON.toJSONString(exportRecord));
        exportRecordService.insert(exportRecord);

        syncReport.exportFileMutilHeader(entity, new RightsReceiveEntity(), IRightsReceiveService.class,
                "selectReceiveList", "权益领取记录",Long.parseLong(exportRecord.getId()),sysUser.getUserName());

        return AjaxResult.success();
    }


    @Override
    public List<RightsReceiveEntity> selectUnPayList(List<String> busiIdList,String startDt) {
//        LambdaQueryWrapper<RightsReceiveEntity> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.in(RightsReceiveEntity::getBusiId,busiIdList);
//        queryWrapper.eq(RightsReceiveEntity::getDelFlag,"0");
//        queryWrapper.eq(RightsReceiveEntity::getSendFlag,"3");
//        queryWrapper.le(RightsReceiveEntity::getCreateTime,startDt);
//        queryWrapper.orderByAsc(RightsReceiveEntity::getCreateTime).last("limit 500");
//        return rightsReceiveMapper.selectList(queryWrapper);
        return rightsReceiveMapper.selectUnPayList(busiIdList,startDt);
    }

    @Override
    public int updateSendFlagById(String id, String sendFlag) {
        return rightsReceiveMapper.updateSendFlagById(id,sendFlag);
    }
}
